package com.gpt.article.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gpt.article.domain.GptGenArticleTask;
import com.gpt.article.domain.GptTopicContent;
import com.gpt.article.mapper.GptGenArticleTaskMapper;
import com.gpt.article.mapper.GptTopicContentMapper;
import com.gpt.article.service.impl.taskHandle.GenTopicArticleTask;
import com.gpt.article.service.impl.taskHandle.TaskSplite;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.zhucx.thread.CrawlerThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gpt.article.mapper.GptTopicTitleMapper;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.service.IGptTopicTitleService;
import org.springframework.util.ObjectUtils;

/**
 * 主题文章Service业务层处理
 * 
 * @author zhuzi
 * @date 2023-11-26
 */
@Service
public class GptTopicTitleServiceImpl implements IGptTopicTitleService 
{

    private static Logger logger = LoggerFactory.getLogger(GptTopicTitleServiceImpl.class);


    @Autowired
    private GptTopicTitleMapper gptTopicTitleMapper;
    @Autowired
    private GptTopicContentMapper gptTopicContentMapper;
    @Autowired
    private GptGenArticleTaskMapper gptGenArticleTaskMapper;

    @Autowired
    private GptGenArticleTaskServiceImpl gptGenArticleTaskServiceImpl;
    private TaskSplite taskSplite;
    /**
     * 查询主题文章
     * 
     * @param id 主题文章主键
     * @return 主题文章
     */
    @Override
    public GptTopicTitle selectGptTopicTitleById(Long id)
    {
        return gptTopicTitleMapper.selectGptTopicTitleById(id);
    }

    /**
     * 查询主题文章列表
     * 
     * @param gptTopicTitle 主题文章
     * @return 主题文章
     */
    @Override
    public List<GptTopicTitle> selectGptTopicTitleList(GptTopicTitle gptTopicTitle)
    {
        return gptTopicTitleMapper.selectGptTopicTitleList(gptTopicTitle);
    }

    /**
     * 新增主题文章
     * 
     * @param gptTopicTitle 主题文章
     * @return 结果
     */
    @Override
    public Long insertGptTopicTitle(GptTopicTitle gptTopicTitle)
    {
        gptTopicTitle.setCreateTime(DateUtils.getNowDate());
        gptTopicTitle.setUpdateTime(DateUtils.getNowDate());
        gptTopicTitleMapper.insertGptTopicTitle(gptTopicTitle);
        return gptTopicTitle.getFileId();
    }

    /**
     * 修改主题文章
     * 
     * @param gptTopicTitle 主题文章
     * @return 结果
     */
    @Override
    public Long updateGptTopicTitle(GptTopicTitle gptTopicTitle)
    {
        gptTopicTitle.setUpdateTime(DateUtils.getNowDate());
        gptTopicTitleMapper.updateGptTopicTitle(gptTopicTitle);
        return gptTopicTitle.getId();
    }

    /**
     * 批量删除主题文章
     * 
     * @param ids 需要删除的主题文章主键
     * @return 结果
     */
    @Override
    public int deleteGptTopicTitleByIds(Long[] ids)
    {
        List<GptTopicTitle> gptTopicTitleList = gptTopicTitleMapper.selectGptTopicTitleListByIds(ids);
        for(GptTopicTitle li:gptTopicTitleList){
            gptTopicContentMapper.deleteGptTopicContentById(li.getContentId());
        }
        return gptTopicTitleMapper.deleteGptTopicTitleByIds(ids);
    }

    /**
     * 删除主题文章信息
     * 
     * @param id 主题文章主键
     * @return 结果
     */
    @Override
    public int deleteGptTopicTitleById(Long id)
    {
        return gptTopicTitleMapper.deleteGptTopicTitleById(id);
    }

    /**
     * 根据id获取待生成所需信息
     * @param ids
     * @return
     */
    public List<GptTopicTitle> getTopicInfoByids(Long[] ids){
        List<GptTopicTitle> topicTitles = gptTopicTitleMapper.selectGptTopicTitleListByIds(ids);
        return topicTitles;
    }
    /**
     * 生成主题文章
     * @param ids
     * @return
     */
    @Override
    public int genGptTopic(Long[] ids,String acc){
        List<GptTopicTitle> topicTitles = gptTopicTitleMapper.selectGptTopicTitleListByIds(ids);
//        synchronized (this) {
            Integer xigaoAllNum = genStart(topicTitles,acc);
//        }
        System.out.println("本批次任务数：："+topicTitles.size());
        return topicTitles.size();
    }

    /**
     * 创建任务
     * @param topicTitles
     * @return
     */
    public Integer genStart(List<GptTopicTitle> topicTitles,String acc ){
        GptGenArticleTask genArticleTask = new  GptGenArticleTask();
        genArticleTask.setProce(0);
        genArticleTask.setArticleNum(topicTitles.size());
        genArticleTask.setStatus("1");
        genArticleTask.setSuccessNum(0);
        genArticleTask.setErrorNum(0);
        genArticleTask.setCreateTime(DateUtils.getNowDate());
        Long taskId =gptGenArticleTaskServiceImpl.insertGptGenArticleTask(genArticleTask);
//         gptGenArticleTaskMapper.insertGptGenArticleTask(genArticleTask);
//        Long taskId = genArticleTask.getFileId();
        //开启任务
        Integer xigaoAllNum = new GenTopicArticleTask().createContentByOutline(taskId,acc,topicTitles);
        return xigaoAllNum;
    }

    /**
     * 大批量生成逻辑
     * @param gptTopicTitle 主题文章
    //     * @param gennum 生成数量
     * @return
     */
    @Override
    public int genManyTopic(GptTopicTitle gptTopicTitle,String acc){
        List<GptTopicTitle> gptTopicTitleList = new ArrayList<GptTopicTitle>();
        boolean findAll = false;
        Integer taskSum = gptTopicTitleMapper.getTopicTitleCount(gptTopicTitle);
        if(gptTopicTitle.getGennum()==0){
            findAll = true;
        }
        if(!findAll){
            taskSum = taskSum>gptTopicTitle.getGennum()?gptTopicTitle.getGennum():taskSum;
        }
        logger.info("当前任务条数:::{}",taskSum);
        //小于200则不分片 直接批量执行即可
        if(taskSum<10&&taskSum>0){
            gptTopicTitleList = gptTopicTitleMapper.selectGptTopicTitleIdsList(gptTopicTitle);
            genStart(gptTopicTitleList,acc);
        }else {
            //本次任务id
            List<Long> ids = gptTopicTitleMapper.selectIds(gptTopicTitle);
            //任务分片
            taskSplite = new TaskSplite.Builder()
                    .setSource(ids)
                    .setAcc(acc)
                    .setSpan(128)//分片间隔，相对应应用来说单次执行量
                    .setTaskType("topic")
                    .build();
            taskSplite.process();
        }

        return taskSum;
    }


    @Override
    public Map<String,Object> getAticleMap(Long contentId){
        Map<String,Object> map = new HashMap<String,Object>();
        map =getContentMap(contentId);
        return map;
    }

    public Map<String,Object> getContentMap(Long contentId){
        Map<String,Object> map = new HashMap<String,Object>();
        GptTopicContent gptTopicContent =  gptTopicContentMapper.selectGptTopicContentById(contentId);
        if(ObjectUtils.isEmpty(gptTopicContent)){
            map.put("artContent","暂未找到相关内容");
            map.put("artTitle","");
            return map;
        }
        String catalogContent = gptTopicContent.getGenCatalog();
        String keyWords = "本文关键词："+gptTopicContent.getKeyWords();
        String content = keyWords+catalogContent+gptTopicContent.getGenContent();
        map.put("artContent",content);
        map.put("artTitle",gptTopicContent.getTitle());
        return map;
    }
}
